#!/usr/bin/env python
# -*- coding:utf-8 -*-

#2011/2/23 17:01
#Compatible with python3.2

from locale import str
import urllib.request
import urllib.error

url = r'http://www.google.com.hk/m/search?site=weather&gl=cn&hl=zh-CN&q=%E6%B7%B1%E5%9C%B3'

try:
    data = urllib.request.urlopen(url).read().decode(encoding="utf-8", errors="strict")
except ValueError:
    print("Bad link. Try to add 'http://' in the front.")
    exit()
except urllib.error.URLError:
    print("Oops! Error ocurs while connecting.")
    exit()

try:
    info = data.split('<p>')[3]
except IndexError:
    print("Parse Error.")
    exit()

#Debug
#print(info)

today = '今日'
mon = '周一'
tue = '周二'
wed = '周三'
thu = '周四'
fri = '周五'
sat = '周六'
sun = '周日'

week = (today, mon, tue, wed, thu, fri, sat, sun) 

#摄氏�?
degree_Celsius = '°C'

for key in week:
    if (info.find(key) == -1):
        continue
    else:
        index_pre = info.find(key)
        step = info[index_pre : ].find(degree_Celsius)
        temp = info[index_pre : (index_pre + step)][len(key) : ]
        print(key + temp + degree_Celsius)

